package com.lime49.lockcrypt.idletimer;
 
import com.sun.jna.*;
import com.sun.jna.win32.*;
/**
 * A timer which obtains the last time of user activity on Wi32 Systems.
 * Requires JNA to work.
 * Original code by ochafik
 */
public class Win32IdleTimer implements IIdleTimer {
	public Win32IdleTimer() {
            
        }
	
	/**
	 * Get the amount of milliseconds that have elapsed since the last input event
	 * (mouse or keyboard)
	 * @return idle time in milliseconds
	 */
	public int getIdleTimeMillis() {
		User32.LASTINPUTINFO lastInputInfo = new User32.LASTINPUTINFO();
		User32.INSTANCE.GetLastInputInfo(lastInputInfo);
		return Kernel32.INSTANCE.GetTickCount() - lastInputInfo.dwTime;
	}
	
	enum State {
		UNKNOWN, ONLINE, IDLE, AWAY
	};

    public interface Kernel32 extends StdCallLibrary {
		Kernel32 INSTANCE = (Kernel32)Native.loadLibrary("kernel32", Kernel32.class);
		
		/**
		 * Retrieves the number of milliseconds that have elapsed since the system was started.
		 * @see http://msdn2.microsoft.com/en-us/library/ms724408.aspx
		 * @return number of milliseconds that have elapsed since the system was started.
		 */
		public int GetTickCount();
	};
	
	public interface User32 extends StdCallLibrary { 
		User32 INSTANCE = (User32)Native.loadLibrary("user32", User32.class);
		
		/**
		 * Contains the time of the last input.
		 * @see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputstructures/lastinputinfo.asp
		 */
		public static class LASTINPUTINFO extends Structure {
			public int cbSize = 8;
			
			/// Tick count of when the last input event was received.
			public int dwTime;
		}
		
		/**
		 * Retrieves the time of the last input event.
		 * @see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/getlastinputinfo.asp
		 * @return time of the last input event, in milliseconds
		 */
		public boolean GetLastInputInfo(LASTINPUTINFO result);
	};
	
	
}